<cfscript>
	// Redirect already logged in users to the secure area
	if(isDefined("SESSION.MySCF")){ Location("secure", false, 302); }

	REQUEST.ERRORS = StructNew();
	if(isDefined("FORM.UserName") && isDefined("FORM.Password")){
		if(Len(FORM.UserName) LT 1){
			REQUEST.ERRORS.UserName = "Enter your Username";
		} else if(Len(FORM.Password) LT 1){
			REQUEST.ERRORS.Password = "Enter your Password";
		} else {
			REQUEST.login = EntityLoad("mySCF", {Username = FORM.Username}, true);
			if(!isDefined("REQUEST.login") || !REQUEST.login.verifyPassword(FORM.Password)){
				REQUEST.ERRORS.Password = "The username or password you entered is incorrect";

			// If the user needs to be activated, redirect to activation page
			} else if(!REQUEST.login.getStatus()){
				Location(
					"signup/activate.cfm?username=" & REQUEST.login.getUsername(),
					false, 302
				);
			} else {
				SESSION.MySCF = StructNew();
				SESSION.MySCF.MySCFID = REQUEST.login.getMySCFID();
				SESSION.MySCF.Username = REQUEST.login.getUsername();
				if(isDefined("SESSION.MySCFApplication")){
					REQUEST.app = SESSION.MySCFApplication;
					StructDelete(SESSION, "MySCFApplication");
					Location(REQUEST.app, false, 302);
				} else {
					Location("secure", false, 302);
				}
			}
		}
	} else {
		param name="FORM.UserName" default="";
	}
</cfscript>
<html>
	<head>
		<title>MySCF Login</title>
		<style type="text/css">
			* { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; }
			table { border: 1px solid black; background-color: #F0F8FF; width: 300px; }
			a { font-size: 10px; }
			.err { color: #f00; text-align: center; font-size: 12px; }
			.signup a { font-size: 15px; }
			.signup { text-align: center; }
		</style>
		<script type="text/javascript" src="/jquery/jquery-1.4.2.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				var un = $("input[name='UserName']");
				if(un.val() == ''){	un.focus(); }
				else { $("input[name='Password']").focus(); }
			});
		</script>
	</head>
	<body>
		<cfoutput>
		<form method="post" action="index.cfm">
			<table>
				<tr>
					<td>Username:</td>
					<td>
						<input type="text" name="UserName" size="25"
							value="#HTMLEditFormat(FORM.UserName)#" />
					</td>
				</tr>
				<cfif StructKeyExists(REQUEST.ERRORS, "UserName")>
				<tr>
					<td>&nbsp;</td>
					<td class="err">#REQUEST.ERRORS.UserName#</td>
				</tr>
				</cfif>
				<tr><td>&nbsp;</td></tr>
				<tr>
					<td>Password:</td>
					<td>
						<input type="password" name="Password" size="25" />
					</td>
				</tr>
				<cfif StructKeyExists(REQUEST.ERRORS, "Password")>
				<tr>
					<td>&nbsp;</td>
					<td class="err">#REQUEST.ERRORS.Password#</td>
				</tr>
				</cfif>
				<tr>
					<td>&nbsp;</td>
					<td style="text-align: right">
						<input type="submit" name="submitbtn" value="Login" />
					</td>
				</tr>
			</table>

			<br/><br/>

			<table>
				<tr>
					<td colspan="2" class="signup">
						<a href="signup">Don't have an account? Sign up!</a>
					</td>
				</tr>
			</table>
		</form>
		</cfoutput>
	</body>
</html>